/*
 * @Description: 订单分货明细DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2021-02-24 11:56:54
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-25 15:09:20
 */
import { FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';

import intl from 'utils/intl';
import moment from 'moment';
import { resetMonth } from '@src/utils/utilsFun';
const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c' : commonConfigStore.O2_OM;
const orderDistributionDtlFormDS = () => ({
    autoQuery: false,
    selection: 'multiple',
    primaryKey: 'id',
    autoCreate: true,
    paging: false,
    transport: {
        read: config => {
            const { quotaOrderDetailId } = config.data
            return {
                ...config,
                data: [],
                url: `${O2_OM}/v1/quota-orders/${quotaOrderDetailId}`,
                method: 'get',
                // transformResponse: res => {
                //     console.log(JSON.parse(res));
                    
                //     return JSON.parse(res).data;
                // },
            };
        },
        create: config => {
            const data = config.data[0];
            console.log(data);

            const { quotaMonth } = data;
            // 截掉月份后面的时间
            const submitData = resetMonth(data, quotaMonth, 'quotaMonth')
            const url = data.type === 'save' ? `${O2_OM}/v1/quota-orders` : `${O2_OM}/v1/quota-orders/submit-work-flow`
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url,
                method: 'post',
            };
        },
        update: config => {
            const data = config.data[0];
            console.log(data);

            const { quotaMonth } = data;
            // 截掉月份后面的时间
            const submitData = resetMonth(data, quotaMonth, 'quotaMonth')

            const url = data.type === 'save' ? `${O2_OM}/v1/quota-orders` : `${O2_OM}/v1/quota-orders/submit-work-flow`
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url,
                method: 'post',
            };
        },
    },
    fields: [
        {
            name: 'quotaCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.quotaCode').d('分货单号'),
        },
        // {
        //     name: 'productObj',
        //     type: FieldType.object,
        //     label: intl.get('dji.otc.om.view.field.tagTypeB').d('产品'),
        //     required: true,
        //     ignore: 'always',
        //     lovCode: 'O2PCM.QUERY_MASTER_SKU_LIST',
        //     lovQueryAxiosConfig: (_code, lovConfig) => {
        //         const { queryUrl } = lovConfig && lovConfig.originData || {};
        //         return {
        //             url: queryUrl,
        //             method: 'post',
        //         };
        //     }
        // },
        // {
        //     name: 'productCode',
        //     bind: 'productObj.skuCode',
        //     type: FieldType.string,
        // },
        // {
        //     name: 'productName',
        //     bind: 'productObj.skuName',
        //     type: FieldType.string,
        // },
        // {
        //     name: 'largeAreaObj',
        //     type: FieldType.object,
        //     label: intl.get('dji.otc.om.view.field.tagName').d('大区'),
        //     required: true,
        //     labelWidth: 120,
        //     checkValueOnOptionsChange: true,
        //     lookupUrl: `${O2_CM}/v1/cm-contract-permissions/get-largeArea-info`,
        //     ignore: 'always',
        // },
        // {
        //     name: 'regionId',
        //     type: FieldType.number,
        //     label: intl.get('dji.otc.om.view.field.tagName').d('大区'),
        //     bind: 'largeAreaObj.value',
        // },
        {
            name: 'regionId',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.region').d('大区'),
            lookupCode: 'DJICM_CHOOSE_BIG_AREA',
            required: true,
        },
        // {
        //     name: 'areaObj',
        //     type: FieldType.object,
        //     label: intl.get('dji.otc.om.view.field.tagName').d('小区'),
        //     required: true,
        //     lookupUrl: `${O2_CM}/v1/cm-contract-permissions/get-area-info`,
        //     ignore: 'always',
        //     // cascadeMap: { parentCodeValue: 'regionId' },
        //     // dynamicProps: {
        //     //     disabled: ({ record }) => {
        //     //         if (!record.get('largeAreaObj')) {
        //     //             record.set('areaObj', null);
        //     //             return true;
        //     //         }
        //     //     },
        //     // },
        // },
        // {
        //     name: 'areaId',
        //     bind: 'areaObj.value',
        //     type: FieldType.string,
        //     label: intl.get('dji.otc.om.view.field.tagName').d('小区'),

        // },
        {
            name: 'areaId',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.area').d('小区'),
            lookupCode: 'DJIOM_CHOOSE_SMALL_AREA',
            required: true,
            disabled: true
            // cascadeMap: { parentCodeValue: 'regionId' },
            // dynamicProps: {
            //     disabled: ({ record }) => {
            //         if (!record.get('regionId')) {
            //             record.set('areaId', null);
            //             return true;
            //         }
            //     },
            // },
        },
        {
            name: 'customerType',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerType').d('客户类型'),
            lookupCode: 'DJISM.CUSTOMER_TYPE',
            required: false,
        },
        {
            name: 'quotaMonth',
            type: FieldType.month,
            label: intl.get('dji.otc.om.view.field.quotaMonth').d('配额月份'),
            required: true,
            defaultValue: moment().format("YYYY MM"),
        },
        {
            name: 'businessGroup',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.groupName').d('组别'),
            lookupCode: 'DJICM_GROUP',
            required: true,
        },
    ],
});

const orderDistributionDtlTableDS = () => ({
    autoCreate: false,
    // dataToJSON: 'normal',
    primaryKey: 'orderDeskProductId',
    paging: false,
    transport: {
        read: config => {

            return {
                ...config,
                url: `${O2_OM}/v1/quota-order-details`,
                method: 'get',
            };
        },
        submit: config => {
            return {
                ...config,
                data: config.data[0],
                url: `${O2_OM}/v1/quota-order-details`,
                method: 'POST',
            };
        },
        destroy: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/quota-order-details`,
                method: 'delete',
            };
        },
    },
    fields: [
        {
            name: 'orderObj',
            type: FieldType.object,
            label: intl.get('dji.otc.om.view.field.orderCode').d('订单编号'),
            required: true,
            lovCode: 'DJIOM.ORDER_DESK_PRODUCT_LIST',
            ignore: 'always'
        },
        {
            name: 'productBom',
            type: FieldType.string,
            required: true,
            bind: 'orderObj.productBom',
        },
        {
            name: 'orderDeskId',
            type: FieldType.number,
            required: true,
            bind: 'orderObj.odId',
        },
        {
            name: 'orderDeskProductId',
            type: FieldType.number,
            required: true,
            bind: 'orderObj.odpId',
        },
        {
            name: 'customerCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerName').d('客户'),
            required: true,
            bind: 'orderObj.customerToken',
            disabled: true,
        },
        {
            name: 'customerType',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerType').d('客户类型'),
            required: true,
            lookupCode: 'DJISM.CUSTOMER_TYPE',
            // disabled: true,
        },
        {
            name: 'productCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productCode').d('料号'),
            required: true,
            disabled: true,
            bind: 'orderObj.productBom',
        },
        {
            name: 'productName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.productName').d('产品名称'),
            required: true,
            disabled: true,
            bind: 'orderObj.skuName',
        },
        {
            name: 'orderQuantity',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.orderQuantity').d('订单数量'),
            required: true,
            disabled: true,
            bind: 'orderObj.number',
        },
        {
            name: 'unallocatedQuantity',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.unallocatedQuantity').d('未分配数量'),
            required: true,
            disabled: true,
            bind: 'orderObj.unallocatedQuantity',
        },
        {
            name: 'suggestedQuantity',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.suggestedQuantity').d('建议数量'),
            required: true,
            disabled: true,
        },
        {
            name: 'quantity',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.thisTimeQuantity').d('本次分配数量'),
            required: true,
        },
        {
            name: 'reason',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.adjustmentReason').d('调整原因'),
            required: true,
        },
        {
            name: 'customerCreditBalance',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.customerCreditBalance').d('客户信用余额'),
            required: true,
            disabled: true,
        },
        {
            name: 'kaAvailableQuantityByArea',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.kaAvailableQuantityByArea').d('KA可发货数量(小区)'),
            required: true,
        },
        {
            name: 'othersAvailableQuantityByArea',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.othersAvailableQuantityByArea').d('其他可发货数量(小区)'),
            required: true,
            disabled: true,
        },
        {
            name: 'kaAvailableQuantityByRegion',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.kaAvailableQuantityByRegion').d('KA可发货数量(大区)'),
            required: true,
            disabled: true,
        },
        {
            name: 'othersAvailableQuantityByRegion',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.othersAvailableQuantityByRegion').d('其他可发货数量(大区)'),
            required: true,
            disabled: true,
        },
        {
            name: 'currentMonthAvailableQuota',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.currentMonthAvailableQuota').d('当月可用计划配额(大区)'),
            required: true,
            disabled: true,
        },
        {
            name: 'currentMonthQuota',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.currentMonthQuota').d('当月计划总配额(大区)'),
            required: true,
            disabled: true,
        },
    ],
});

const approvalDS = () => ({
    autoQuery: false,
    selection: false,
    paging: false,
    primaryKey: 'id',
    transport: {
        read: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/cle-charge-apply-workflow/flowPreviewRecords`,
                method: 'get',
            };
        },
    },
    fields: [
       
        { name: 'fileKey', type: FieldType.string },
        {
            name: 'NodeName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.currentSteps').d('当前步骤'),
        },
        {
            name: 'Approver',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.currentHandler').d('当前处理人'),
        },
        {
            name: 'ApprovalStatus',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.treatmentResults').d('处理结果'),
        },
        {
            name: 'ApprovalContent',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.handlingOpinions').d('处理意见'),
        },
        {
            name: 'CreateTime',
            type: FieldType.date,
            label: intl.get('dji.otc.om.view.field.lastHandleTime').d('最后处理时间'),
        },
    ],
});

const recommendedQuantityDateSet = () => ({
    autoQuery: false,
    selection: false,
    paging: false,
    transport: {
        read: config => {
            const { djiRecommendedQuantityDTOList } = config.data
            return {
                ...config,
                data: djiRecommendedQuantityDTOList,
                url: `${O2_OM}/v1/quota-order-details/query-recommended-quantity`,
                method: 'post',
            };
        },
    },
});

const submitDateSet = () => ({
    autoQuery: false,
    transport: {
        submit: config => {
            const data = config.data[0];
            const { quotaMonth } = data[0];
            // 截掉月份后面的时间
            const submitData = resetMonth(data[0], quotaMonth, 'quotaMonth')
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url: `${O2_OM}/v1/quota-orders/submit-work-flow`,
                method: 'post',
            };
        },
    },
});

export { orderDistributionDtlFormDS, orderDistributionDtlTableDS, approvalDS, recommendedQuantityDateSet, submitDateSet };
